<?php
class Daily extends Admin_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('fund_model','fund');
		$this->load->model('voucher_model','voucher');
		$this->load->model('voucher_balance_model','balance');
		$this->load->model('voucher_check_model','check');
		$this->load->model('voucher_receive_paid_model','receive_paid');
		$this->load->model('voucher_detail_model','voucher_detail');
	}
	
	//**** สมุดรายวันทั่วไป ******
	function index($fund_id)
	{
		//$this->db->debug = TRUE;
		$user = login_data();
		$agency_id = (level_data('view'))?(isset($_GET['agency_id']))?$_GET['agency_id']:$user['agency_id']:$user['agency_id'];
		//**************Start where section ******************
		$where = '';
		list($year,$month,$data['year'],$data['month']) = last_voucher_date($fund_id);
		$data['agency_id'] = $agency_id;
		list($start_date,$end_date) = year_budget($year);
		$where = ' and vouchers.voucher_date between \''.$start_date.'\' and \''.$end_date.'\'';
		if(!empty($month))$where = ' and YEAR(vouchers.voucher_date) = '.$year.' and MONTH(vouchers.voucher_date) = '.$month;
		if(!empty($agency_id))$where .= ' and vouchers.agency_id = '.$agency_id;
		if(!empty($_GET['voucher_type_id']))
		{
			$where .= ' and vouchers.voucher_type_id = '.$_GET['voucher_type_id'];
		}
		else
		{
			$where .= ' and vouchers.voucher_type_id in (1,2,3)';
		}
		//**************End where section ******************
		
		$data['fund'] = $this->fund->get_row($fund_id);
		//$data['result'] = $this->voucher_detail->where('vouchers.is_balance is null and vouchers.mode is null and vouchers.voucher_type_id = 3 and vouchers.fund_id = '.$fund_id.$where)->order_by('voucher_date asc,id asc')->get();
		$data['result'] = $this->voucher_detail->where('vouchers.is_balance is null and vouchers.mode is null and vouchers.fund_id = '.$fund_id.$where)->order_by('voucher_date asc,id asc')->get();
		$this->template->append_metadata(js_print());
		$this->template->build('voucher_daily',$data);
	}
	
	
}
?>